package com.dongdongqiang.admin.service;

import com.dongdongqiang.admin.dao.SysPermissionMapper;
import com.dongdongqiang.admin.enums.CacheName;
import com.dongdongqiang.common.base.BaseService;
import com.dongdongqiang.common.base.BaseTreeVO;
import com.dongdongqiang.common.cache.CacheComponent;
import com.dongdongqiang.domain.sys.SysPermission;
import com.dongdongqiang.domain.sys.SysRole;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by ZZB on 2017/10/26.
 */

@Service
public class SysPermissionService extends BaseService<SysPermission>{
    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    @Autowired
    private CacheComponent cacheComponent;
    @Autowired
    private SysRoleService sysRoleService;

    public BaseTreeVO<SysPermission> buildTreeVOByMap(Map<String, SysPermission> permissionMap) {
        Collection<SysPermission> list = permissionMap.values();

        //构建parentMap
        Map<String , List<SysPermission>> parentMap = new HashMap<>();
        for (SysPermission permission : list) {
            String parentId = permission.getParentId();
            if(StringUtils.isBlank(parentId)){
                parentId = "root";
            }
            if(!parentMap.containsKey(parentId)){
                parentMap.put(parentId,new ArrayList<>());
            }
            parentMap.get(parentId).add(permission);
        }

        BaseTreeVO<SysPermission> rootTreeVO = buildRootTreeVO();
        List<SysPermission> root = parentMap.get("root");
        for (SysPermission sysPermission : root) {
            BaseTreeVO<SysPermission> treeVO = buildTreeVO1(rootTreeVO,sysPermission,parentMap,permissionMap);
            rootTreeVO.getChildren().add(treeVO);
        }


        return rootTreeVO;
    }

    public BaseTreeVO<SysPermission> buildRootTreeVO(){
        BaseTreeVO<SysPermission> treeVO = new BaseTreeVO<>();
        treeVO.setRoot(true);
        treeVO.setChildren(new ArrayList<>());
        treeVO.setItem(null);
        treeVO.setLevel(0);
        treeVO.setOrder(0);
        treeVO.setParentId(null);
        treeVO.setName("root");
        return treeVO;
    }

    public BaseTreeVO<SysPermission> buildTreeVO( BaseTreeVO<SysPermission> parent,SysPermission item,
                                                  Map<String , List<SysPermission>> parentMap){
        BaseTreeVO<SysPermission> treeVO = new BaseTreeVO<>();
        treeVO.setParentId(parent.getItem()==null?"":parent.getItem().getId());
        treeVO.setName(item.getPermissionName());
        treeVO.setOrder(item.getOrderIndex());
        treeVO.setItem(item);
        treeVO.setRoot(false);
        treeVO.setChildren(new ArrayList<>());

        List<SysPermission> children = parentMap.get(item.getId());
        if(CollectionUtils.isNotEmpty(children)){
            for (SysPermission child : children) {
                BaseTreeVO<SysPermission> childTreeVO = buildTreeVO(treeVO, child, parentMap);
                treeVO.getChildren().add(childTreeVO);
            }
        }

        return treeVO;
    }

    public BaseTreeVO<SysPermission> buildTreeVO1(BaseTreeVO<SysPermission> parent,SysPermission item,
                                                  Map<String , List<SysPermission>> parentMap,
                                                  Map<String, SysPermission> map){
        Set<String> builtSet = new HashSet<>();
        Map<String,List<BaseTreeVO<SysPermission>>> builtParentVOMap = new HashMap<>();
        SysPermission thisItem = item;
        if(!builtParentVOMap.containsKey(thisItem.getParentId())){
            builtParentVOMap.put(thisItem.getParentId(),new ArrayList<>());
        }

        while (true){
            builtSet.add(thisItem.getId());
            if(!builtParentVOMap.containsKey(thisItem.getId())){
                builtParentVOMap.put(thisItem.getId(),new ArrayList<>());
            }
            SysPermission unBuiltChild = null;
            if(parentMap.containsKey(thisItem.getId())){
                unBuiltChild = parentMap.get(thisItem.getId()).stream()
                        .filter(sysPermission -> !builtSet.contains(sysPermission.getId()))
                        .findFirst().orElse(null);
            }
            if(unBuiltChild != null){
                thisItem = unBuiltChild;
                continue;
            } else {
                BaseTreeVO<SysPermission> treeVO = new BaseTreeVO<>();
                treeVO.setRoot(false);
                treeVO.setItem(thisItem);
                treeVO.setOrder(thisItem.getOrderIndex());
                treeVO.setName(thisItem.getPermissionName());
                treeVO.setParentId(thisItem.getParentId());
                treeVO.setLevel(thisItem.getLevel());
                treeVO.setChildren(builtParentVOMap.get(thisItem.getId()));
                builtParentVOMap.get(thisItem.getParentId()).add(treeVO);

                if(thisItem.getId().equals(item.getId())){
                    return treeVO;
                }
                thisItem = map.get(thisItem.getParentId());
            }
        }
    }

    public List<SysPermission> queryByRoleId(String roleId){
        return sysPermissionMapper.queryByRoleId(roleId);
    }

}
